package com.cn;

import com.cn.dao.UserDao;
import com.cn.io.Resources;
import com.cn.pojo.UserBean;
import com.cn.sqlSession.SqlSession;
import com.cn.sqlSession.SqlSessionFactory;
import com.cn.sqlSession.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;

public class Test {

    public static void main(String[] args) throws Exception {
        InputStream in = Resources.getResourceAsStream("sqlMapperConfiger.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserBean user = new UserBean();
        user.setId(2);
        user.setUsername("tom");

        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<UserBean> all = userDao.findAll();
        for (UserBean userBean : all){
            System.out.println(userBean);
        }
        System.out.println("=======================");
        UserBean user1 = userDao.findByCondition(user);
        System.out.println(user1);


    }

    @org.junit.Test
    public void testUpdate() throws Exception{
        InputStream in = Resources.getResourceAsStream("sqlMapperConfiger.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserBean user = new UserBean();
        user.setId(5);
        user.setUsername("wangwu");
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        Integer i = userDao.updateById(user);
        System.out.println(i);
    }

    @org.junit.Test
    public void testAdd() throws Exception{
        InputStream in = Resources.getResourceAsStream("sqlMapperConfiger.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserBean user = new UserBean();
        user.setId(6);
        user.setUsername("张三");
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        Integer i = userDao.add(user);
        System.out.println(i);
    }

    @org.junit.Test
    public void testDel() throws Exception{
        InputStream in = Resources.getResourceAsStream("sqlMapperConfiger.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserBean user = new UserBean();
        user.setId(6);

        UserDao userDao = sqlSession.getMapper(UserDao.class);

        Integer i = userDao.delById(user);
        System.out.println(i);
    }
}
